<template>
  <el-table
        ref="tableRef"
        :data="tableData"
        v-bind="tableDefaultAttribute"
        row-key="id"
        :max-height="tableHeight"
        table-layout="auto"
    >
        <template #empty>
            <span>无数据</span>
        </template>
        <el-table-column type="selection" width="55" :align="'center'" />
        <template v-for="col in tableColumns" :key="col.prop">
            <el-table-column
                v-if="!col.isHidden"
                :align="col.align"
                :prop="col.prop"
                :label="col.label"
                :fixed="col.fixed"
                :formatter="col.formatter"
                :type="col.type"
                :width="col.width"
            >
                <template
                    v-if="col.formFieldItemType === 'boolean'"
                    #default="scope"
                >
                    <el-switch
                        v-model="scope.row[`${col.prop}`]"
                        :active-value="'Y'"
                        :inactive-value="'N'"
                    />
                </template>
            </el-table-column>
        </template>
    </el-table>
    <el-button-group class="bg">
        <el-button class="btn" type="primary" @click="closeWindow"
            >关闭</el-button
        >
    </el-button-group>
    <div class="page">
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[50, 100, 150, 200]"
            layout="sizes, prev, pager, next"
            :total="totalnum"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
    </div>
</template>
<script setup lang="ts" name="AuditDetail">
import { ref, reactive, onMounted } from 'vue';
import {
    ElTable,
} from 'element-plus';
import {
    isEmpty,
} from 'lodash';
import {
    tableDefaultAttribute,
    useTableInfo,
    tableColumnDefault,
} from '@/hooks/useTable';
import type { TableColumn } from '@/common/types/app';
import { Request } from '@/app/request';
import { tableMaxHeightAdjust } from '@/common/const';
import type {
    TableInstance,
} from 'element-plus';

import { AuditDetailViewModel } from '@/common/types/business';
// const { cntrCorp, cntrOwner, cntrDam, cyCfs } = useAppStore();
const tableRef = ref<TableInstance>();
const tableData = ref<Array<AuditDetailViewModel>>([]);
const tableHeight = ref(100);
const tableColumns = reactive<TableColumn[]>([
    {
        ...tableColumnDefault,
        order: 1,
        prop: 'pay_currency',
        label: '实收付货币',
        sortable: 'true',
        width: 150,
        
    },    
    {
        ...tableColumnDefault,
        order: 2,
        prop: 'pay_amount',
        label: '实收付金额',
        width: 150,
    },

    {
        ...tableColumnDefault,
        order: 3,
        prop: 'feedet_currency',
        label: '应收付货币',
        sortable: 'true',
        width: 150,
    },
    {
        ...tableColumnDefault,
        order: 3,
        prop: 'feedet_amount',
        label: '应收付金额',
        sortable: 'true',
        width: 150,
    },
    {
        ...tableColumnDefault,
        order: 3,
        prop: 'currency_rate',
        label: '汇率',
        sortable: 'true',
        width: 150,
    },
    
    {
        ...tableColumnDefault,
        order: 9,
        prop: 'force_flag',
        label: '强制核销',
        formFieldItemType: 'boolean',
        width: 100
    },
    {
        ...tableColumnDefault,
        order: 7,
        prop: 'remark',
        label: '备注',
        sortable: 'custom',
        width: 400,
    },
]);

const props = defineProps<{
    auditNo?: string;
}>();
const emit = defineEmits<{
    close: [];
}>();
let queryWhere = {
    where: {
        audit_no: {
            equals: props.auditNo,
        },
    },
};

const onRefresh = async () => {
    const req = new Request('核销明细查询', {
        ...queryWhere,
    });
    const r = await req.post();

    totalnum.value = r.ext;
    tableData.value.length = 0;

    if (!isEmpty(r.obj)) {
        tableData.value = r.obj;
    }
};
const {
    currentPage,
    pageSize,
    totalnum,
    handleSizeChange,
    handleCurrentChange,
} = useTableInfo(onRefresh);
onMounted(() => {
    onRefresh();
    tableHeight.value =
        window.innerHeight -
        Math.ceil(tableRef.value?.$el.getBoundingClientRect().bottom) +
        tableMaxHeightAdjust;
});
const closeWindow = () => {
    emit('close');
}
</script>
<style lang="scss" scoped>
.bg {
    margin-top: 10px;

    .btn {
        margin-right: 10px;
    }
}

.page {
    float: right;
    margin-top: 10px;
}
</style>
